package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.domain.dao.CoreEquipmentCheckMapper;
import com.corechina.apex_osswords.domain.dao.CoreEquipmentTypeMapper;
import com.corechina.apex_osswords.domain.dto.CoreEquipmentTypeDto;
import com.corechina.apex_osswords.domain.entity.CoreEquipmentCheck;
import com.corechina.apex_osswords.domain.entity.CoreEquipmentType;
import com.corechina.apex_osswords.domain.vo.CoreEquipmentTypeVo;
import com.corechina.apex_osswords.service.ICoreEquipmentTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 设备分类表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-12-09
 */
@Service
public class CoreEquipmentTypeServiceImpl extends ServiceImpl<CoreEquipmentTypeMapper, CoreEquipmentType> implements ICoreEquipmentTypeService {

    @Autowired
    private CoreEquipmentTypeMapper coreEquipmentTypeMapper;

    @Autowired
    private CoreEquipmentCheckMapper coreEquipmentCheckMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response add(CoreEquipmentTypeDto coreEquipmentTypeDto) {
        CoreEquipmentType coreEquipmentType = new CoreEquipmentType();
        BeanUtils.copyProperties(coreEquipmentTypeDto, coreEquipmentType);
        coreEquipmentType.setEnterpriseId(ShiroUtils.getEnterpriseId());
        coreEquipmentTypeMapper.insert(coreEquipmentType);
        if (coreEquipmentTypeDto.getCoreEquipmentCheckList().size() > 0) {
            for (CoreEquipmentCheck coreEquipmentCheck : coreEquipmentTypeDto.getCoreEquipmentCheckList()) {
                coreEquipmentCheck.setEquipmentType(coreEquipmentType.getId());
                coreEquipmentCheckMapper.insert(coreEquipmentCheck);
            }
        }
        return Response.ok();
    }

    @Override
    public Response update(CoreEquipmentTypeDto coreEquipmentTypeDto) {
        CoreEquipmentType coreEquipmentType = new CoreEquipmentType();
        BeanUtils.copyProperties(coreEquipmentTypeDto, coreEquipmentType);
        coreEquipmentType.setEnterpriseId(null);
        coreEquipmentTypeMapper.updateById(coreEquipmentType);
        coreEquipmentCheckMapper.deleteByEquipmentType(coreEquipmentTypeDto.getId());
        if (coreEquipmentTypeDto.getCoreEquipmentCheckList().size() > 0) {
            for (CoreEquipmentCheck coreEquipmentCheck : coreEquipmentTypeDto.getCoreEquipmentCheckList()) {
                coreEquipmentCheck.setEquipmentType(coreEquipmentType.getId());
                coreEquipmentCheckMapper.insert(coreEquipmentCheck);
            }
        }
        return Response.ok();

    }

    @Override
    public Response getList(String name, Integer pageIndex, Integer pageSize) {
        IPage<CoreEquipmentTypeVo> equipmentTypeIPage = coreEquipmentTypeMapper.getList(new Page(pageIndex, pageSize), name, ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId());
        return Response.ok(equipmentTypeIPage.getRecords(), equipmentTypeIPage.getTotal());
    }

}
